package com.example.yichuangyun.mvvm.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.config.Constant;
import com.example.yichuangyun.databinding.ItemRiskChildBinding;
import com.example.yichuangyun.databinding.ItemRiskInfoBinding;
import com.example.yichuangyun.model.company.OutsideInvestDataModel;
import com.example.yichuangyun.mvvm.ui.activity.CompanyDetailsActivity;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.JumpIntent;
import com.example.yichuangyun.utils.RequestUtils;

import java.util.List;

/**
 * 产权证书
 */
public class OutsideInvestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<OutsideInvestDataModel> data;

    public OutsideInvestAdapter(Context context, List<OutsideInvestDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRiskInfoBinding binding = ItemRiskInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            OutsideInvestDataModel model = data.get(position);
            if (position == 0) { // 经营异常
                myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getList().size()));
                myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getList(), position));
                myViewHolder.binding.ivArrow.setVisibility(model.getList().size() > 0 ? View.VISIBLE : View.GONE);
                myViewHolder.binding.tvNoData.setVisibility(model.getList().size() > 0 ? View.GONE : View.VISIBLE);
                myViewHolder.binding.tvCount.setVisibility(model.getList().size() > 0 ? View.VISIBLE : View.GONE);
            }
            myViewHolder.binding.recyclerView.setVisibility(model.isOpen() ? View.VISIBLE : View.GONE);
            myViewHolder.binding.ivArrow.setImageResource(model.isOpen() ? R.mipmap.icon_up : R.mipmap.icon_down);
            myViewHolder.binding.tvTitle.setText(model.getTitle());

            myViewHolder.binding.llTitle.setEnabled(data.size() > 0);
            myViewHolder.binding.llTitle.setOnClickListener(view -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(holder.getAbsoluteAdapterPosition());
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemRiskInfoBinding binding;

        public MyViewHolder(ItemRiskInfoBinding itemRiskInfoBinding) {
            super(itemRiskInfoBinding.getRoot());
            binding = itemRiskInfoBinding;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<OutsideInvestDataModel.OutsideInvestModel> mData;
        private final int parentPosition;

        public MyAdapter(List<OutsideInvestDataModel.OutsideInvestModel> mData, int parentPosition) {
            this.mData = mData;
            this.parentPosition = parentPosition;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemRiskChildBinding binding = ItemRiskChildBinding.inflate(LayoutInflater.from(context), parent, false);
            return new ManageViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ManageViewHolder) {
                ManageViewHolder myViewHolder = (ManageViewHolder) holder;
                OutsideInvestDataModel.OutsideInvestModel model = mData.get(position);
                if (model != null) {
                    if (parentPosition == 0) { // 经营异常
                        myViewHolder.binding.tvText7.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.invest_company, CommonUtil.setText3(model.getInvestName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.invest_legal_person, CommonUtil.setText3(model.getInvestOperName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.invest_set_up_date, CommonUtil.setText3(model.getInvestStartDate())), Html.FROM_HTML_MODE_LEGACY));
                        if (!TextUtils.isEmpty(model.getStockPercent())) {
                            myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.invest_ratio, CommonUtil.setText3(CommonUtil.getResultForDouble(Double.parseDouble(model.getStockPercent())))), Html.FROM_HTML_MODE_LEGACY));
                        }
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.invest_date, CommonUtil.setText3(model.getShouldConDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.invest_money, CommonUtil.setText3(CommonUtil.getResultForDouble(model.getShouldCapiConv()))), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText7.setText(Html.fromHtml(context.getString(R.string.invest_status, CommonUtil.setText3(model.getInvestStatus())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText1.setOnClickListener(view -> {
                            if (CommonUtil.isCanClick()) {
                                Bundle bundle = new Bundle();
                                bundle.putString(Constant.eidKey, model.getInvestEid());
                                JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
                            }
                        });
                    }
                }
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class ManageViewHolder extends RecyclerView.ViewHolder {

        ItemRiskChildBinding binding;

        public ManageViewHolder(ItemRiskChildBinding itemRiskChildBinding) {
            super(itemRiskChildBinding.getRoot());
            binding = itemRiskChildBinding;
        }
    }
}
